package com.svmc.app.task.activity;

import java.util.ArrayList;

import luyenlv.pro.screen.base.BaseActivity;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.svmc.app.task.adapter.ListSelectRingAdapter;
import com.svmc.app.task.adapter.RingtoneAdapter;
import com.svmc.app.task.model.RingtoneModel;
import com.svmc.app.task.utils.CommonDefine;

public class SelectRingtone extends BaseActivity implements OnClickListener,CommonDefine{
	public static int isplaying = -1;
	ListView ls;
	ArrayList<RingtoneModel> listRing;
	int edit_or_new;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		this.getWindow().setBackgroundDrawable(
				new ColorDrawable(Color.argb(0, 255, 255, 255)));
		setContentView(getLayout(R.array.select_ringtone));
		
		findViewById(R.id.selectring_img_back).setOnClickListener(this);
		findViewById(R.id.selectring_img_done).setOnClickListener(this);
		ls = (ListView)findViewById(R.id.selectring_ls_lisviewtring);
		Bundle bundle_finish = getIntent().getExtras();
		edit_or_new =bundle_finish.getInt(EDIT_OR_NEW);
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		updateListAudio();
	}

	@Override
	public void clearMemory() {
		
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.selectring_img_back:
			finish();
			break;
		case R.id.selectring_img_done:
			
			if(listRing.size() >0){
				Intent selectRingIntent = new Intent();
				selectRingIntent.putExtra("titleRing", listRing.get(ListSelectRingAdapter.mSelectedVariation).getTitle());
				selectRingIntent.putExtra("pathRing", listRing.get(ListSelectRingAdapter.mSelectedVariation).getPath());
				selectRingIntent.putExtra(EDIT_OR_NEW, edit_or_new);
//				setResult(RESULT_OK, selectRingIntent);
//				finish();
				if (getParent() == null) {
				    setResult(Activity.RESULT_OK, selectRingIntent);
				} else {
				    getParent().setResult(Activity.RESULT_OK, selectRingIntent);
				}
				finish();
			}else {
				showToast("no ringtone files to selected");
				finish();
			}
			
			
			break;
		default:
			break;
		}
		
	}
	public void updateListAudio (){
		RingtoneAdapter ring = new RingtoneAdapter(this);
		listRing = ring.getAllAudioSDcard();
		if(listRing.size() ==0){
			showToast("Not available audio in your Phone");
		}else {
			ListSelectRingAdapter myadapter = new ListSelectRingAdapter(this, listRing,0);
			ls.setAdapter(myadapter);
			
			
		}
		
	}
	public void showToast (String text){
		Toast.makeText(this, text, 0).show();
	}
	
	public void listenClickListView() {
		
		ls.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long id) {
				//showToast(listRing.get(position).getPath());
			}

		});
	}

}
